﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web.Areas.Admin.Controllers
{
    public class ShopController : Controller
    {
        Manager.BLL.Admin.M_ShopBll bll = new Manager.BLL.Admin.M_ShopBll();

        //
        // GET: /Admin/Shop/
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "view")]
        public ActionResult Index()
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

            var query = from s in bll.GetModelList("")
                        select new Models.Shop
                        {
                            ShopID=s.ShopID,
                            ShopDesc=s.ShopDesc,
                            ShopTypeName = categoryBll.GetModel(s.ShopTypeID.Value).TypeName,
                            ShopAddress=s.ShopAddress,
                            ShopTel=s.ShopTel,
                            ShopImage=s.ShopImage,
                            ShopImage1=s.ShopImage1,
                            ShopName=s.ShopName,
                            IsTop=s.IsTop?"是":"否",
                            IsValid=s.IsValid?"是":"否"
                        };
            return View(query.ToList<Models.Shop>());
        }

        [HttpPost]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "view")]
        public ActionResult Index(FormCollection forms)
        {
            try
            {
                Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

                string ShopID = forms["tbxSearchShopID"];
                string ShopName = forms["tbxSearchShopName"];
                string ShopAddress = forms["tbxSearchShopAddress"];
                string ShopTel = forms["tbxSearchShopTel"];
                string ShopTypeName = forms["tbxSearchShopTypeName"];
                string ShopIsTop = forms["tbxSearchIsTop"];
                string ShopIsValid = forms["tbxSearchIsValid"];

                var query = from s in bll.GetModelList("")
                            select new Models.Shop
                            {
                                ShopID = s.ShopID,
                                ShopDesc = s.ShopDesc,
                                ShopTypeName = categoryBll.GetModel(s.ShopTypeID.Value).TypeName,
                                ShopAddress = s.ShopAddress,
                                ShopTel = s.ShopTel,
                                ShopImage = s.ShopImage,
                                ShopImage1 = s.ShopImage1,
                                ShopName = s.ShopName,
                                IsTop = s.IsTop ? "是" : "否",
                                IsValid = s.IsValid ? "是" : "否"
                            };
                if (ShopID.Length > 0)
                    query = query.Where(s => s.ShopID == int.Parse(ShopID));
                if (ShopName.Length > 0)
                    query = query.Where(s => s.ShopName.Contains(ShopName));
                if (ShopAddress.Length > 0)
                    query = query.Where(s => s.ShopAddress.Contains(ShopAddress));
                if (ShopTel.Length > 0)
                    query = query.Where(s => s.ShopTel.Contains(ShopTel));
                if (ShopTypeName.Length > 0)
                    query = query.Where(s => s.ShopTypeName.Contains(ShopTypeName));
                if (ShopIsTop.Length > 0)
                    query = query.Where(s => s.IsTop.Equals(ShopIsTop));
                if (ShopIsValid.Length > 0)
                    query = query.Where(s => s.IsValid.Equals(ShopIsValid));

                return View(query.ToList<Models.Shop>());
            }
            catch {
                Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

                var query = from s in bll.GetModelList("")
                            select new Models.Shop
                            {
                                ShopID = s.ShopID,
                                ShopDesc = s.ShopDesc,
                                ShopTypeName = categoryBll.GetModel(s.ShopTypeID.Value).TypeName,
                                ShopAddress = s.ShopAddress,
                                ShopTel = s.ShopTel,
                                ShopImage = s.ShopImage,
                                ShopImage1 = s.ShopImage1,
                                ShopName = s.ShopName,
                                IsTop = s.IsTop ? "是" : "否",
                                IsValid = s.IsValid ? "是" : "否"
                            };
                return View(query.ToList<Models.Shop>());
            }
        }


        [Filters.AdminAuthFilter(ModuleID = 8, Ope = "add")]
        public ActionResult Add()
        {
            BindSelect();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "add")]
        public ActionResult Add(Manager.Model.Admin.M_Shop model,FormCollection forms)
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();

            try
            {
                if (!categoryBll.Exists(model.ShopTypeID.Value))
                    ModelState.AddModelError("", Resources.Global.msg_select_exist);

                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length!=0)
                    {
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "Shop");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("ShopImage", urlImage);
                        }
                        else
                        {
                            model.ShopImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    int r = bll.Add(model);
                    if (r > 0)
                        return RedirectToAction("Index", "Shop");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }
        /// <summary>
        /// 门店类别下拉列表框
        /// </summary>
        public void BindSelect()
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();
            IEnumerable<SelectListItem> categorys = from c in categoryBll.GetModelList("ModuleID=8") select new SelectListItem { Value = c.TypeID.ToString(), Text = c.TypeName };
            ViewBag.CategorySelect = categorys;
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "edit")]
        public ActionResult Edit(int id)
        {
            if (!bll.Exists(id))
                return RedirectToAction("Index");
            BindSelect();
            return View(bll.GetModel(id));
        }

        [HttpPost]
        [ValidateInput(false)]
        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "edit")]
        public ActionResult Edit(int id, Manager.Model.Admin.M_Shop model,FormCollection forms)
        {
            Manager.BLL.Admin.M_CategoryBll categoryBll = new Manager.BLL.Admin.M_CategoryBll();
            model.ShopID = id;
            if (!bll.Exists(id))
                ModelState.AddModelError("", Resources.Global.msg_select_exist);
            else
                model.ShopImage = bll.GetModel(id).ShopImage;
            try
            {
                //上传
                if (Request.Files.Count > 0)
                {
                    HttpPostedFileBase fileImage = Request.Files[0];
                    if (fileImage.FileName.Length != 0)
                    {
                        bool res = (new Manager.Tools.FileUpload()).DeleteExistFile(Server.MapPath("~"+model.ShopImage));
                        string urlImage = (new Manager.Tools.FileUpload()).SaveFile(fileImage, "Shop");
                        if (urlImage.IndexOf("/") == -1)
                        {
                            ModelState.AddModelError("ShopImage", urlImage);
                        }
                        else
                        {
                            model.ShopImage = urlImage;
                        }
                    }
                }

                if (ModelState.IsValid)
                {
                    if (!categoryBll.Exists(model.ShopTypeID.Value))
                        ModelState.AddModelError("", Resources.Global.msg_select_exist);
                    model.ShopDesc = forms["ShopDesc"];
                    bool r = bll.Update(model);
                    if (r)
                        return RedirectToAction("Index", "Shop");
                    else
                        ModelState.AddModelError("", Resources.Global.msg_error);
                }
            }
            catch
            {
                ModelState.AddModelError("", Resources.Global.msg_error);
            }
            BindSelect();
            return View(model);
        }

        [Filters.AdminAuthFilter(ModuleID = (int)Manager.Tools.AuthPermission.ModuleTypes.ShopSet, Ope = "del")]
        public ActionResult Del(int id)
        {
            if (!bll.Exists(id))
                return Content("<script>alert('" + Resources.Global.msg_select_exist + "');location.href='/Admin/Shop/Index';</script>");
            try
            {
                bool r = bll.Delete(id);
                if (r)
                    return Content("<script>alert('" + Resources.Global.msg_success + "');location.href='/Admin/Shop/Index';</script>");
                else
                    return Content("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Shop/Index';</script>");

            }
            catch (Exception)
            {
                return JavaScript("<script>alert('" + Resources.Global.msg_error + "');location.href='/Admin/Shop/Index';</script>");
            }
        }
    }


}
